package com.woniuxy.provider.student.controller;

import com.woniuxy.common.DTO.ResponseResult;
import com.woniuxy.common.entity.Student;
import com.woniuxy.provider.student.service.StudentService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/1/19 15:13
 * @FileName: StudentController
 * @version: 1.0
 * @Description: 描述
 * ` @RefreshScope 数据刷新注解
 */
//@RefreshScope
@Log4j2
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    StudentService studentService;
    @Value("${server.port}")
    int value;

    @GetMapping("/{id}")
    public ResponseResult<Student> getStudent(@PathVariable("id") int id) {
        Student student = studentService.getStudent(id);
        return new ResponseResult<>(200, "查询成功"+value, student);
    }

    @PutMapping()
    public ResponseResult<?> updateStudent(@RequestBody Student student) {
        int row = studentService.updateStudent(student);
        return new ResponseResult<>(200, "修改成功", row);
    }

    @PostMapping
    public ResponseResult<?> addStudent(@RequestBody Student student) {
        log.info("student:"+student);
        int row = studentService.addStudent(student);
        return new ResponseResult<>(200, "新增成功", row);

    }

    @DeleteMapping("/{id}")
    public ResponseResult<?> deleteStudent(@PathVariable("id") int id) {
        int row = studentService.deleteStudent(id);
        return new ResponseResult<>(200, "删除成功", row);
    }

    @GetMapping("/")
    public ResponseResult<List<Student>> getStudent() {
        List<Student> list = studentService.getStudentList();
        return new ResponseResult<>(200, "获取列表", list);
    }


    @GetMapping("/timeout")
    public ResponseResult<?> timeout() {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ResponseResult<>(200, "获取列表", "超时测试");
    }



    @GetMapping("/ok")
    public ResponseResult<?> ok() {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ResponseResult<>(200, "获取列表", "ok服务降级测试");

    }

    @Value("${testConfig}")
    String testConfig;
    @GetMapping("/testConfig")
    public ResponseResult<String> testConfig() {
        return new ResponseResult<>(200, "获取列表", testConfig);
    }
}
